package com.lgc.javathread.volatileTest;

public class Main {
    public static volatile int found = 0;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("1start");

                while (0 == found) { //上边的如果不加volatile修饰时，found被其它线程改变时，当前线程读不到，值只会一直是0
                }

                System.out.println("1end");
            }
        }, "thread1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("2start");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("2end");
                change();
            }
        }, "thread2").start();
    }

    public static void change() {
        found = 1;
    }
}
